﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AppManagement
{
    #region Using
    using DevComponents.DotNetBar;
    using AppManagement.UsersManager;
    using AppManagement.Helpers;
    using AppManagement.Repositories;
    using AppManagement.UserManagers;
    using AppManagement.BusManagers;
    using AppManagement.SearchManagers;
    using AppManagement.Reports; 
    #endregion

    public partial class MainForm : Office2007Form 
    {
        public MainForm()
        {
            InitializeComponent();
            InitControls();
        }

        private void menQuanLyChucNang_Click(object sender, EventArgs e)
        {
            FrmQuanLyChucNang frm = new FrmQuanLyChucNang();
            frm.ShowDialog();
        }

        private void menQuanLyChucNangNhomNguoiDung_Click(object sender, EventArgs e)
        {
            frmQuanLyChucNangNhomNguoiDung frm = new frmQuanLyChucNangNhomNguoiDung();
            frm.ShowDialog();
        }

        private void menQuanLyLoaiNguoiDung_Click(object sender, EventArgs e)
        {
            FrmQuanLyLoaiNguoiDung frm = new FrmQuanLyLoaiNguoiDung();
            frm.ShowDialog();
        }

        private void menQuanLyNguoiDung_Click(object sender, EventArgs e)
        {
            FrmQuanLyNguoiDung frm = new FrmQuanLyNguoiDung();
            frm.ShowDialog();
        }

        private void menDangXuat_Click(object sender, EventArgs e)
        {
            menDangNhap.Enabled = true;
            GlobalVars.g_CurrentUser = null;

            FrmDangNhap frm = new FrmDangNhap();
            frm.Handler += ValidateLogin;
            frm.ShowDialog();
        }

        public void InitControls()
        {
            // Nhan vien
            menQuanLyChucNang.Enabled = false;
            menQuanLyChucNangNhomNguoiDung.Enabled = false;
            menQuanLyLoaiNguoiDung.Enabled = false;
            menQuanLyNguoiDung.Enabled = false;
            menQuanLyNhanVien.Enabled = false;
        }

        public void ValidateLogin(object sender, EventArgs e)
        {
            bool success = true;
            string infoText = "";
            var eventType = (TransferSimpleDataEvent)e;
            string[] comp = eventType.Data.Split(new char[] { '#' });

            string username = comp[0].Trim();
            string password = comp[1].Trim();
            string PasswordEnc = Encryption.Encrypt(password);

            GlobalVars.g_CurrentUser = NguoiDungRepository.SelectByUserName(username);

            if (GlobalVars.g_CurrentUser == null)
            {
                success = false;
                infoText = "Người dùng này chưa có trong hệ thống";
            }
            else
            {
                GlobalVars.g_CurrentUser = NguoiDungRepository.SelectByUserNameAndPassword(username, PasswordEnc);
                if (GlobalVars.g_CurrentUser == null)
                {
                    success = false;
                    infoText = "Bạn đã nhập sai mật khẩu";
                }
                else
                {
                    infoText = "Chào bạn  " + GlobalVars.g_CurrentUser.TenDangNhap + "  vào hệ thống !";
                }
            }

            if (!success)
            {
                MessageBoxEx.Show(infoText, "Log In", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                // Re-login
                FrmDangNhap frm = new FrmDangNhap();
                frm.Handler += ValidateLogin;
                frm.ShowDialog();
            }
            else
            {
                menDangNhap.Enabled = false;

                // Get current user
                GlobalVars.g_CurrentUser = NguoiDungRepository.SelectByUserName(username);
                // Mapping name of controls with specifed enums
                var dictFucns = GlobalUserManagers.InitFuncsMapping();
                // Store list of functionalities that user allow using
                var funcsAllowUsing = GlobalUserManagers.InitFuncsAllowUsing();

                // Disable some functions that user not allow using
                foreach (var item in dictFucns)
                {
                    foreach (var item2 in funcsAllowUsing)
                    {
                        if (item.Value == item2)
                        {
                            GlobalUserManagers.EnableFuncAllowUsing(pnlMain, item.Key);
                            break;
                        }
                    }
                }

                MessageBoxEx.Show(infoText);
            }
        }

        private void menDangNhap_Click(object sender, EventArgs e)
        {
            FrmDangNhap frm = new FrmDangNhap();
            frm.Handler += ValidateLogin;
            frm.ShowDialog();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            string ErrorText = "";

            if (!System.IO.File.Exists(GlobalVars.g_strPathConfig))
            {
                FrmKetNoiCSDL frm = new FrmKetNoiCSDL();
                frm.ShowDialog();
            }
            else
            {
                XMLProvider _provider = new XMLProvider();
                // Load config file to get infos that used to connect to DB
                _provider.GetConnectionInfo(ref ErrorText);
                if (ErrorText != "")
                {
                    MessageBoxEx.Show(ErrorText, "ERROR", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                    // Re-create connection infos
                    FrmKetNoiCSDL frm = new FrmKetNoiCSDL();
                    frm.ShowDialog();
                }
                else
                {
                    // Get connection string successfull, then open connection
                    var lstNguoiDung = NguoiDungRepository.SelectAll();
                    if (lstNguoiDung.Count > 0)
                    {
                        FrmDangNhap frm = new FrmDangNhap();
                        frm.Handler += ValidateLogin;
                        frm.ShowDialog();
                    }
                    else
                    {
                        // Chua co nguoi dung nao trong he thong, nguoi dung dau tien se giu vai tro la admin
                        menDangNhap.Enabled = false;
                        menDangXuat.Enabled = false;
                        menDoiMatKhau.Enabled = false;
                        menQuanLyChucNang.Enabled = true;
                        menQuanLyChucNangNhomNguoiDung.Enabled = true;
                        menQuanLyLoaiNguoiDung.Enabled = true;
                        menQuanLyNguoiDung.Enabled = true;
                    }
                }
            }
        }

        private void menDoiMatKhau_Click(object sender, EventArgs e)
        {
            FrmDoiMatKhau frm = new FrmDoiMatKhau();
            frm.ShowDialog();
        }

        private void menThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void menQuanLyNhanVien_Click(object sender, EventArgs e)
        {
            FrmQuanLyNhanVien frm = new FrmQuanLyNhanVien();
            frm.ShowDialog();
        }

        private void menQuanLyBenhNhan_Click(object sender, EventArgs e)
        {
            FrmQuanLyBenhNhan frm = new FrmQuanLyBenhNhan();
            frm.ShowDialog();
        }

        private void menQuanLyThuoc_Click(object sender, EventArgs e)
        {
            FrmQuanLyThuoc frm = new FrmQuanLyThuoc();
            frm.ShowDialog();
        }

        private void menQuanLyDonViTinh_Click(object sender, EventArgs e)
        {
            FrmQuanLyDonViTinh frm = new FrmQuanLyDonViTinh();
            frm.ShowDialog();
        }

        private void menQuanLyBenhPham_Click(object sender, EventArgs e)
        {
            FrmQuanLyBenhPham frm = new FrmQuanLyBenhPham();
            frm.ShowDialog();
        }

        private void menQuanLyNhomDichVu_Click(object sender, EventArgs e)
        {
            FrmQuanLyNhomDichVu frm = new FrmQuanLyNhomDichVu();
            frm.ShowDialog();
        }

        private void menQuanLyDonViChiSo_Click(object sender, EventArgs e)
        {
            FrmQuanLyDonViChiSo frm = new FrmQuanLyDonViChiSo();
            frm.ShowDialog();
        }

        private void menQuanLyChiSo_Click(object sender, EventArgs e)
        {
            FrmQuanLyChiSo frm = new FrmQuanLyChiSo();
            frm.ShowDialog();
        }

        private void menQuanLyDichVu_Click(object sender, EventArgs e)
        {
            FrmQuanLyDichVu frm = new FrmQuanLyDichVu();
            frm.ShowDialog();
        }

        private void menSaoLuuDuLieu_Click(object sender, EventArgs e)
        {
            if (backupDialog.ShowDialog() == DialogResult.OK)
            {
                DBProvider provider = new DBProvider();
                var sqlQuery = "BACKUP DATABASE " + GlobalVars.g_strDataBaseName + " TO DISK = '" + backupDialog.FileName.ToString() + "'";

                // Open connection
                provider.InitDBProvider(GlobalVars.g_strDataSource, GlobalVars.g_strDataBaseName);

                var ds = new DataSet();
                provider.SqlQuery = sqlQuery;
                provider.FillDataSet(ref ds);

                MessageBoxEx.Show("Sao lưu dữ liệu thành công!", "BACKUP COMPLETED", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
                return;
        }

        private void menPhucHoiDuLieu_Click(object sender, EventArgs e)
        {
            if (restoreDialog.ShowDialog() == DialogResult.OK)
            {
                DBProvider provider = new DBProvider();
                var sqlQuery = "USE master RESTORE DATABASE " + GlobalVars.g_strDataBaseName + " FROM DISK = '" + restoreDialog.FileName.ToString() + "'";

                // Open connection
                provider.InitDBProvider(GlobalVars.g_strDataSource, GlobalVars.g_strDataBaseName);

                var ds = new DataSet();
                provider.SqlQuery = sqlQuery;
                provider.FillDataSet(ref ds);

                MessageBoxEx.Show("Phục hồi dữ liệu thành công!", "RESTORE COMPLETED", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
                return;
        }

        private void menLapPhieuKham_Click(object sender, EventArgs e)
        {
            FrmQuanLyBenhNhan frm = new FrmQuanLyBenhNhan(true);
            frm.Handler += GetBenhNhan;
            frm.ShowDialog();
        }

        public void GetBenhNhan(object sender, EventArgs e)
        {
            var eventType = (TransferSimpleDataEvent)e;
            string[] comp = eventType.Data.Split(new char[] { '#' });

            FrmLapPhieuKham frm = new FrmLapPhieuKham(comp[0]);
            frm.ShowDialog();
        }

        private void menTimBenhNhan_Click(object sender, EventArgs e)
        {
            FrmTimBenhNhan frm = new FrmTimBenhNhan();
            frm.Handler += TimKiemBenhNhan;
            frm.ShowDialog();
        }

        public void TimKiemBenhNhan(object sender, EventArgs e)
        {
            var eventType = (TransferSimpleDataEvent)e;
            string[] comp = eventType.Data.Split(new char[] { '#' });

            FrmQuanLyBenhNhan frm = new FrmQuanLyBenhNhan(comp[0], comp[1], comp[2]);
            frm.ShowDialog();
        }

        private void menTimBacSi_Click(object sender, EventArgs e)
        {
            FrmTimBacSi frm = new FrmTimBacSi();
            frm.Handler += TimKiemBacSi;
            frm.ShowDialog();
        }

        public void TimKiemBacSi(object sender, EventArgs e)
        {
            var eventType = (TransferSimpleDataEvent)e;
            string[] comp = eventType.Data.Split(new char[] { '#' });

            FrmQuanLyNhanVien frm = new FrmQuanLyNhanVien(comp[0], comp[1], comp[2]);
            frm.ShowDialog();
        }

        private void ttbtnBacSi_Click(object sender, EventArgs e)
        {
            FrmQuanLyNhanVien frm = new FrmQuanLyNhanVien();
            frm.ShowDialog();
        }

        private void ttbtnBenhNhan_Click(object sender, EventArgs e)
        {
            FrmQuanLyBenhNhan frm = new FrmQuanLyBenhNhan();
            frm.ShowDialog();
        }

        private void ttbtnLapPhieuKham_Click(object sender, EventArgs e)
        {
            FrmQuanLyBenhNhan frm = new FrmQuanLyBenhNhan(true);
            frm.Handler += GetBenhNhan;
            frm.ShowDialog();
        }

        private void ttbtnNhomDichVu_Click(object sender, EventArgs e)
        {
            FrmQuanLyNhomDichVu frm = new FrmQuanLyNhomDichVu();
            frm.ShowDialog();
        }

        private void ttbtnDichVu_Click(object sender, EventArgs e)
        {
            FrmQuanLyDichVu frm = new FrmQuanLyDichVu();
            frm.ShowDialog();
        }

        private void menBaoCaoDoanhSoTheoNgay_Click(object sender, EventArgs e)
        {
            FrmBaoCaoDoanhSoTheoNgay frm = new FrmBaoCaoDoanhSoTheoNgay();
            frm.ShowDialog();
        }

        private void menBaoCaoDoanhSoTheoBacSi_Click(object sender, EventArgs e)
        {
            FrmBaoCaoDoanhSoTheoBacSy frm = new FrmBaoCaoDoanhSoTheoBacSy();
            frm.ShowDialog();
        }

        private void menBaoCaoDoanhSoChiTiet_Click(object sender, EventArgs e)
        {
            FrmBaoCaoDoanhSoChiTiet frm = new FrmBaoCaoDoanhSoChiTiet();
            frm.ShowDialog();
        }
    }
}
